package com.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.Result;
import com.common.log.OperationLog;
import com.common.log.OperationType;
import com.entity.Clock;
import com.entity.JobLevel;
import com.service.JobLevelService;
import com.service.sys.UserVoService;
import com.vo.sys.UserVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author clay
 * @since 2022-03-27
 */
@RestController
@RequestMapping("/jobLevel")
public class JobLevelController {

    @Resource
    private JobLevelService jobLevelService;

    @ApiOperation("添加职称等级信息")
    @OperationLog(operationType = OperationType.INSERT, describe = "添加职称等级信息", entity = JobLevel.class, primaryKey = "id")
    @PostMapping(value = "/add")
    public Result addDepartment(@RequestBody @Valid JobLevel jobLevel) {
        jobLevelService.save(jobLevel);
        return Result.success();
    }

    @Resource
    private UserVoService userVoService;
    @ApiOperation("删除职称等级信息")
    @OperationLog(operationType = OperationType.DELETE, describe = "删除职称等级", entity = JobLevel.class, primaryKey = "id")
    @GetMapping(value = "/delete/{id}")
    public Result deleteDepartment(@PathVariable("id") String id) {
        List<UserVo> userVoList = userVoService.list(new QueryWrapper<UserVo>().eq(
                "job_level_id", id
        ));
        if (CollectionUtil.isNotEmpty(userVoList)) {
            return Result.fail("存在被引用的信息，无法删除");
        }
        jobLevelService.removeById(id);
        return Result.success();
    }

    @ApiOperation("通过id获取职业等级信息")
    @GetMapping(value = "/info/{id}")
    public Result info(@PathVariable("id") String id) {
        return Result.success(jobLevelService.getById(id));
    }

    @ApiOperation("查询打卡信息")
    @PostMapping(value = "/select")
    public Result selectDepartment(@RequestBody JobLevel jobLevel) {
        QueryWrapper<JobLevel> wrapper = new QueryWrapper<>();
        wrapper.eq(
                !StringUtils.isEmpty(jobLevel.getEnabled()),
                "enabled",
                jobLevel.getEnabled()
        ).eq(
                !StringUtils.isEmpty(jobLevel.getTitleLevel()),
                "title_level",
                jobLevel.getTitleLevel()
        ).like(
                !StringUtils.isEmpty(jobLevel.getName()),
                "name",
                jobLevel.getName()
        );
        return Result.success(jobLevelService.list(wrapper));
    }

    @ApiOperation("修改职称等级")
    @PostMapping("/update")
    @OperationLog(operationType = OperationType.UPDATE, describe = "修改职称等级", entity = JobLevel.class, primaryKey = "id")
    public Result update(@RequestBody JobLevel jobLevel) {
        return Result.success(jobLevelService.updateById(jobLevel));
    }
}
